extends Node
class_name ModMapParseUtil
# ==================================================
# author: lxl
# description：
# ==================================================



# 解析场景资源
static func parse_tscn(path,top_json):
	var scene:Node2D = load(path).instance()
	var tscn_json = {}
	top_json["map"][scene.name] = tscn_json
	var background = parse_background(scene,top_json)
	var mask = parse_mask(scene,top_json)
	var collision = parse_collision(scene,top_json)
	var specail_collision = parse_specail_collision(scene,top_json)
	var conveyor_belt = parse_conveyor_belt(scene,top_json)
	var ledge = parse_ledge(scene,top_json)
	var hight_level = parse_hight_level(scene,top_json)
	var foot_print = parse_foot_print(scene,top_json)
	var state_action_move = parse_state_action_move(scene,top_json)
	var deep_water = parse_deep_water(scene,top_json)
	var audio = parse_audio(scene,top_json)
	var entounter_poke = parse_entounter_poke(scene,top_json)
	var ice_block_switch_consumer = parse_ice_block_switch_consumer(scene,top_json)
	var ice_block_switch_emit = parse_ice_block_switch_area_emit(scene,top_json)
	var deep_bubble_area_special_efect = parse_deep_bubble_area_special_effect(scene,top_json)
	var dust_grass_special_effect = parse_dust_grass_special_effect(scene,top_json)
	var tall_grass_special_effect = parse_tall_grass_special_effect(scene,top_json)
	var light_grass_special_effect = parse_light_grass_special_effect(scene,top_json)
	var all_grass = parse_all_grass(scene,top_json)
	var rotate_npc = parse_rotate_npc(scene,top_json)
	var mask_special_node = parse_mask_special_node(scene,top_json)
	var block_btn = parse_block_btn(scene,top_json)
	var block_collision = parse_block_collision(scene,top_json)
	var active = parse_active_block(scene,top_json)
	var lights = parse_light(scene,top_json)
	var stone_switch = parse_stone_switch(scene,top_json)
	
	if !background.empty():tscn_json["background"] = background
	if !mask.empty():tscn_json["mask"] = mask
	if !hight_level.empty():tscn_json["hight_level"] = hight_level
	if collision.size() > 0: tscn_json["collision"] = collision
	if specail_collision.size() > 0: tscn_json["specail_collision"] = specail_collision
	if conveyor_belt.size() > 0: tscn_json["conveyor_belt"] = conveyor_belt
	if ledge.size() > 0: tscn_json["ledge"] = ledge
	if foot_print.size() > 0: tscn_json["foot_print"] = foot_print
	if state_action_move.size() > 0: tscn_json["state_action_move"] = state_action_move
	if deep_water.size() > 0: tscn_json["deep_water"] = deep_water
	if audio.size() > 0: tscn_json["audio"] = audio
	if entounter_poke.size() > 0: tscn_json["entounter_poke"] = entounter_poke
	if ice_block_switch_consumer.size() > 0: tscn_json["ice_block_switch_consumer"] = ice_block_switch_consumer
	if ice_block_switch_emit.size() > 0: tscn_json["ice_block_switch_emit"] = ice_block_switch_emit
	if all_grass.size() > 0: tscn_json["all_grass"] = all_grass
	if rotate_npc.size() > 0: tscn_json["rotate_npc"] = rotate_npc
	if mask_special_node.size() > 0: tscn_json["mask_special_node"] = mask_special_node
	if block_btn.size() > 0: tscn_json["block_btn"] = block_btn
	if block_collision.size() > 0: tscn_json["block_collision"] = block_collision
	if deep_bubble_area_special_efect.size() > 0: tscn_json["deep_bubble_area_special_efect"] = deep_bubble_area_special_efect
	if dust_grass_special_effect.size() > 0: tscn_json["dust_grass_special_effect"] = dust_grass_special_effect
	if tall_grass_special_effect.size() > 0: tscn_json["tall_grass_special_effect"] = tall_grass_special_effect
	if light_grass_special_effect.size() > 0: tscn_json["light_grass_special_effect"] = light_grass_special_effect
	if active.size() > 0: tscn_json["active"] = active
	if lights.size() > 0: tscn_json["light"] = lights
	if stone_switch.size() > 0: tscn_json["stone_switch"] = stone_switch
static func parse_background(scene:Node2D,top_json)->Dictionary:
	if !scene.has_node("Background"): return {}
	# 这里只解析background层
	var background = scene.get_node("Background")
	if background == null: return {}
	var result = deep_parse_no_script(background,top_json)
	return result
static func parse_mask(scene,top_json):
	if !scene.has_node("Mask"): return {}
	# 这里只解析background层
	var mask = scene.get_node("Mask")
	if mask == null: return {}
	var result = deep_parse_no_script(mask,top_json)
	return result

# 解析碰撞
# 碰撞只能用tilemap来控制
# 而且碰撞只能是高层和底层，及只能两层
static func parse_collision(scene,top_json:Dictionary)->Array:
	if !scene.has_node("Collision"): return []
	var collision:Node2D = scene.get_node("Collision")
#	if collision == null: return []
	var node_item = []
	for child in collision.get_children():
		if !(child is TileMap): 
			printerr("碰撞的设置，其下只能有一层，且该层为tilemap,其碰撞的layer只能是2或者6，不能同时为2,6,请检查~")
			continue
		if child.tile_set == null:
			printerr("错误，此tilemap没有设置tileset,请设置full_single.tres资源为tilemap的tileset资源")
			continue
		# 这里验证以下collision_layer是否符合
#		var char_bit = ModParseUtil.num_to_bit(child.collision_layer)
		if !((child.collision_layer != 2 || child.collision_layer != 32) and !(child.collision_layer == 2 && child.collision_layer == 32)):
			printerr("其碰撞Collision的layer只能是2或者6，不能同时为2和6,请检查~")
			continue
		var tilemap_info = ModParseUtil.parse_tilemap(child)
		ModParseUtil.try_parse_resource(child,tilemap_info,top_json)
		node_item.append(tilemap_info)
	return node_item


# 解析单向碰撞，及某个方向拥有碰撞效果
# 这个里面也是tilemap ,跟collision一样，这个不需要读取脚本
# 操作内容跟collision一样，但是也需要复制一份代码出来
static func parse_specail_collision(scene,top_json:Dictionary)->Array:
	if !scene.has_node("SpecailCollision"): return []
	var specail_collision:Node2D = scene.get_node("SpecailCollision")
#	if specail_collision == null : return []
	var node_item = []
	for child in specail_collision.get_children():
		if !(child is TileMap): 
			printerr("单向碰撞的设置，其下只能有一层，且该层为tilemap,其碰撞的layer只能4")
			continue
		if child.tile_set == null:
			printerr("错误，此tilemap没有设置tileset,请设置center_single4.tres资源为tilemap的tileset资源")
			continue
		# 这里验证以下collision_layer是否符合
		if child.collision_layer != 8:
			printerr("其单向碰撞SpecailCollision的tilemap的layer只能是4~")
			continue
		if child.script == null:
			printerr("错误，单向碰撞SpecailCollision检测必须挂载脚本:SpecialCollision.gd,并设置其参数")
			continue
		var tilemap_info = ModParseUtil.parse_tilemap(child)
		ModParseUtil.try_parse_resource(child,tilemap_info,top_json)
		# 除了读取这些还要读取独有的脚本变量值
		var script_json = {}
		script_json["down"] = child.down
		script_json["up"] = child.up
		script_json["left"] = child.left
		script_json["right"] = child.right
		tilemap_info["script_json"] = script_json
		node_item.append(tilemap_info)
	return node_item
	pass

# 解析传送带，及冰面->,单项水路等等
# 必须是此格式
static func parse_conveyor_belt(scene,top_json:Dictionary)->Array:
	
	# 传送带，一层area，并且area下有一层tilemap
	if !scene.has_node("ConveyorBelt"): return []
	var conveyorbelt:Node2D = scene.get_node("ConveyorBelt")
	var node_item = []
	for item_area in conveyorbelt.get_children():
		if !item_area is Area2D: 
			printerr("错误，传送带层必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为传送带组件的检测区域")
			continue
		var char_bit = ModParseUtil.num_to_bit(item_area.collision_layer)

		if char_bit[8] != 1:
			printerr("错误，你必须设置ConveyorBelt的area2d节点collision_layer为9")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		var child = []
		var script_json = {}
		area_info["child"] = child
		area_info["script_json"] = script_json
		for tilemap in item_area.get_children():
			if !(tilemap is TileMap): continue
			var tilemap_info = ModParseUtil.parse_tilemap(tilemap)
			ModParseUtil.try_parse_resource(tilemap,tilemap_info,top_json)
			child.append(tilemap_info)
		# 读取脚本信息
		script_json["out"] = item_area.out
		script_json["ignore_player_machine_states"] = item_area.ignore_player_machine_states
		script_json["dialog_id"] = item_area.dialog_id
		node_item.append(area_info)
	return node_item

# 解析跳跃的悬崖，及玩家从某个方向过去的时候需要进行跳跃
static func parse_ledge(scene,top_json:Dictionary)->Array:
	if !scene.has_node("Ledge"): return []
	var ledge:Node2D = scene.get_node("Ledge")
	
	var node_item = []
	for item_area in ledge.get_children():
		if !item_area is Area2D: 
			printerr("错误，传送带层必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为跳跃的区域")
			continue
		var char_bit = ModParseUtil.num_to_bit(item_area.collision_layer)
		if char_bit[2] != 1:
#		if item_area.collision_layer != 4:
			printerr("错误，你必须设置 Ledge 的area2d节点collision_layer 为 3")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		var child = []
		var script_json = {}
		area_info["child"] = child
		area_info["script_json"] = script_json
		for tilemap in item_area.get_children():
			if !(tilemap is TileMap): continue
			var tilemap_info = ModParseUtil.parse_tilemap(tilemap)
			ModParseUtil.try_parse_resource(tilemap,tilemap_info,top_json)
			child.append(tilemap_info)
		# 读取脚本信息
		script_json["jump_vector"] = [item_area.jump_vector.x,item_area.jump_vector.y]
		node_item.append(area_info)
	return node_item
	pass

# 解析高层开关，这个自带有脚本
static func parse_hight_level(scene,top_json:Dictionary)->Dictionary:
	if !scene.has_node("HightLevel"): return {}
	var hight_level:Node2D = scene.get_node("HightLevel")
	
	var info = {}
	var node_item = []
	info["child"] = node_item
	for item_area in hight_level.get_children():
		if !item_area is Area2D: 
			printerr("错误，传送带层必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为碰撞层级切换的区域")
			continue
		if item_area.collision_mask != 16:
			printerr("错误，你必须设置HightLevel 的area2d节点collision_mask 为 5 ")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		var child = []
		area_info["child"] = child
		for tilemap in item_area.get_children():
			if !(tilemap is TileMap): continue
			var tilemap_info = ModParseUtil.parse_tilemap(tilemap)
			ModParseUtil.try_parse_resource(tilemap,tilemap_info,top_json)
			child.append(tilemap_info)
		# 这里要存一下名字
		area_info["name"] = item_area.name
		node_item.append(area_info)
	var script_json = {}
	info["script_json"] = script_json
	
	script_json["switch_area"] = str(hight_level.switch_area)
	script_json["hight_level_area"] = str(hight_level.hight_level_area)
	script_json["from_level"] = hight_level.from_level
	script_json["to_level"] = hight_level.to_level
	return info


# 解析会生成沙漠的脚印信息布局
static func parse_foot_print(scene,top_json:Dictionary)->Array:
	if !scene.has_node("FootPrint"): return []
	var foot_print:Node2D = scene.get_node("FootPrint")
	
	var node_item = []
	for item_area in foot_print.get_children():
		if !item_area is Area2D: 
			printerr("错误，传送带层必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为脚印的区域")
			continue
		if item_area.collision_mask != 64:
			printerr("错误，你必须设置FootPrint 的area2d节点collision_mask 为 7")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		var child = []
		var script_json = {}
		area_info["child"] = child
		for tilemap in item_area.get_children():
			if !(tilemap is TileMap): continue
			var tilemap_info = ModParseUtil.parse_tilemap(tilemap)
			ModParseUtil.try_parse_resource(tilemap,tilemap_info,top_json)
			child.append(tilemap_info)
		# 读取脚本信息
		node_item.append(area_info)
	return node_item

# 解析移动区域的允许可行走信息
static func parse_state_action_move(scene,top_json:Dictionary)->Array:
	if !scene.has_node("StateActionMove"): return []
	var state_action_move:Node2D = scene.get_node("StateActionMove")
	
	var node_item = []
	for item_area in state_action_move.get_children():
		if !item_area is Area2D: 
			printerr("错误，可行走层必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为行走的区域")
			continue
		if !((item_area.collision_layer != 1 || item_area.collision_layer != 2048) and !(item_area.collision_layer == 1 && item_area.collision_layer == 2048)):
			printerr("错误，你必须设置StateActionMove 的area2d节点collision_layer 为 1 或者12 ，且不能同时设置")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		var child = []
		var script_json = {}
		area_info["child"] = child
		area_info["script_json"] = script_json
		for tilemap in item_area.get_children():
			if !(tilemap is TileMap): continue
			var tilemap_info = ModParseUtil.parse_tilemap(tilemap)
			ModParseUtil.try_parse_resource(tilemap,tilemap_info,top_json)
			child.append(tilemap_info)
		# 读取脚本信息
		script_json["allowed_states"] = item_area.allowed_states
		script_json["state_inner_states"] = item_area.state_inner_states
		script_json["auto_change"] = item_area.auto_change
		script_json["has_active_level"] = item_area.has_active_level
		script_json["dialog_id"] = item_area.dialog_id
		script_json["collision_layer"] = item_area.collision_layer
		node_item.append(area_info)
	return node_item
	pass

# 解析深水区域的范围，这个有一些特殊
static func parse_deep_water(scene,top_json:Dictionary)->Array:
	if !scene.has_node("DeepWater"): return []
	var deep_ater:Node2D = scene.get_node("DeepWater")
	
	var node_item = []
	for item_area in deep_ater.get_children():
		if !item_area is Area2D: 
			printerr("错误，传送带层必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为深水的区域")
			continue
		if item_area.collision_layer != 1048576:
			printerr("错误，你必须设置DeepWater 的area2d节点collision_layer 为 21")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		var child = []
		var script_json = {}
		area_info["child"] = child
		area_info["script_json"] = script_json
		for tilemap in item_area.get_children():
			if !(tilemap is TileMap): continue
			var tilemap_info = ModParseUtil.parse_tilemap(tilemap)
			ModParseUtil.try_parse_resource(tilemap,tilemap_info,top_json)
			child.append(tilemap_info)
		# 读取脚本信息
		script_json["dialog_id"] = item_area.dialog_id
		script_json["base_position"] = [item_area.base_position.x,item_area.base_position.y]
		script_json["map_id"] = item_area.map_id
		script_json["fiexd_transmit"] = item_area.fiexd_transmit
		script_json["fiexd_position"] = [item_area.fiexd_position.x,item_area.fiexd_position.y]
		node_item.append(area_info)
	return node_item
	pass

# 解析音乐播放，也就是地图上的小区域，例如110号道路
static func parse_audio(scene,top_json:Dictionary)->Array:
	if !scene.has_node("Audio"): return []
	var audio:Node2D = scene.get_node("Audio")
	
	var node_item = []
	for item_area in audio.get_children():
		if !item_area is Area2D: 
			printerr("错误，音乐区域必须是一个area+CollisionPolygon2Dp组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个CollisionPolygon2D来作为跳跃的区域")
			continue
		if item_area.collision_mask != 64 && item_area.collision_mask != 131072 :
			printerr("错误，你必须设置Audio 的area2d节点collision_mask 为 7",",",item_area.collision_mask)
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		area_info["name"] = item_area.name
		var child = []
		var script_json = {}
		area_info["child"] = child
		area_info["script_json"] = script_json
		for polygon2d in item_area.get_children():
			if !(polygon2d is CollisionPolygon2D): 
				printerr("错误，音乐区域的 area2d子节点必须是 CollisionPolygon2D")
				continue
			var tilemap_info = ModParseUtil.parse_collision_polygon2d(polygon2d)
			
			child.append(tilemap_info)
		# 读取脚本信息
		script_json["music_area_id"] = item_area.music_area_id
		node_item.append(area_info)
	return node_item
	pass

# 解析遇怪区域
static func parse_entounter_poke(scene,top_json)->Array:
	if !scene.has_node("EncounterPoke"): return []
	var encounter_poke:Node2D = scene.get_node("EncounterPoke")
	
	var node_item = []
	for item_area in encounter_poke.get_children():
		if !item_area is Area2D: 
			printerr("错误，遇怪区域必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为遇怪的区域")
			continue
		
		if item_area.collision_layer != 262144:
			printerr("错误，你必须设置EncounterPoke 的area2d节点collision_layer 为 19")
			continue
		if !((item_area.collision_mask != 64 || item_area.collision_mask != 131072) and !(item_area.collision_mask == 64 && item_area.collision_mask == 131072)):
			printerr("错误，你必须设置EncounterPoke 的area2d节点collision_layer 为 7 或者18 ，且不能同时设置")
			continue
		
		var area_info = ModParseUtil.parse_area2d(item_area)
		var child = []
		var script_json = {}
		area_info["child"] = child
		area_info["script_json"] = script_json
		for tilemap in item_area.get_children():
			if !(tilemap is TileMap): continue
			var tilemap_info = ModParseUtil.parse_tilemap(tilemap)
			ModParseUtil.try_parse_resource(tilemap,tilemap_info,top_json)
			child.append(tilemap_info)
		# 读取脚本信息
		script_json["poke_id"] = item_area.poke_id
		
		node_item.append(area_info)
	return node_item
	pass

# 解析碰撞后旋转npc的按钮，
static func parse_rotate_npc(scene,top_json)->Array:
	if !scene.has_node("RotateNpc"): return []
	var rotate_npc:Node2D = scene.get_node("RotateNpc")
	var node_item = []
	for item in rotate_npc.get_children():
		# 主要是记录一下，脚本的变量信息
		var item_json = {}
		item_json["type"] = "Node2D"
		item_json["position"] = [item.position.x,item.position.y]
		item_json["scale"] = [item.scale.x,item.scale.y]
		item_json["z_index"] = item.z_index
		item_json["visible"] = item.visible
		item_json["modulate"] = [item.modulate.r,item.modulate.g,item.modulate.b,item.modulate.a]
		
		
		var script_json = {}
		item_json["script"] = script_json
		script_json["parent_map"] = str(item.parent_map)
		script_json["npc_start_position"] = [item.npc_start_position.x,item.npc_start_position.y]
		script_json["npcs"] = item.npcs
		script_json["clockwise"] = item.clockwise
		
		var texture = ModParseUtil.resource_parse(item.texture)
		item_json["texture"] = texture
		
		
		
		node_item.append(item_json)
		pass
	return node_item

# 跟下面的一样效果,冰面发射器一样的东西
static func parse_ice_block_switch_area_emit(scene,top_json)->Array:
	if !scene.has_node("IceBlockSwitchEmit"): return []
	var ice_block_emit:Node2D = scene.get_node("IceBlockSwitchEmit")
	var node_item = []
	for item_area in ice_block_emit.get_children():
		if !item_area is Area2D: 
			printerr("错误，IceBlockSwitchEmit 的子项必须是 area2d")
			continue
		if item_area.get_child_count() <= 0:
			printerr("错误，area2d必须存在子项")
			continue
		# 必须 area2d  + tilemap
		if !((item_area.collision_mask != 64 || item_area.collision_layer != 131072) and !(item_area.collision_layer == 64 && item_area.collision_layer == 131072)):
			printerr("错误，你必须设置IceBlockSwitchEmit 的area2d节点collision_mask 为 7或者 18，且不能同时设置")
			continue
		var area_json = ModParseUtil.parse_area2d(item_area)
		var child = []
		area_json["child"] = child
		var script_json = {}
		area_json["script_json"] = script_json
		for child_item in item_area.get_children():
			if child_item is TileMap:
				var tilemap_json = ModParseUtil.parse_tilemap(child_item)
				ModParseUtil.try_parse_resource(child_item,tilemap_json,top_json)
				child.append(tilemap_json)
				pass
			pass
		script_json["sence_parent"] = str(item_area.sence_parent)
		script_json["pos_tilemap"] = str(item_area.pos_tilemap)
		script_json["transmit_position"] = [item_area.transmit_position.x,item_area.transmit_position.y]
		script_json["map_id"] = item_area.map_id
		script_json["layer_level"] = item_area.layer_level
		script_json["on_texture"] = ModParseUtil.try_parse_resource2(item_area.on_texture,top_json)
		script_json["off_texture"] = ModParseUtil.try_parse_resource2(item_area.off_texture,top_json)
		
		node_item.append(area_json)
		pass
	
	return node_item
# 针对grass特效的开关控制检测
# 及这种特效全部踩掉之后，这里对应的节点会消失的那种
# 且必须为以下名字，否则不会加入识别中
# 结构是这样的 Node2d[IceBlockSwitch]
	#				Sprite/Tilemap[这个作为背景]
	#				Collision
	#				ConveyorBelt
	# 其子类可以支持 碰撞，传送带。tilemap,sprite目前仅允许这4个，
static func parse_ice_block_switch_consumer(scene,top_json)->Array:
	# 这个是针对 ， 冰面的传送用的
	if !scene.has_node("IceBlockSwitchConsumer"): return []
	var ice_block_switch_consumer:Node2D = scene.get_node("IceBlockSwitchConsumer")
	var node_item = []
	# 所有的子项必须是node2d
	for item in ice_block_switch_consumer.get_children():
		var temp_json = {
			"bg":[]
		}
		temp_json["script_json"] = {}
		temp_json["script_json"]["ice_path"] = str(item.ice_path)
		
		if !item is Node2D: 
			printerr("错误！，IceBlockSwitch的第一层子级必须是Node2d")
			continue
		if item.get_child_count() <= 0: 
			printerr("错误！，IceBlockSwitch必须要拥有对应机关限制的子级，否则请清除此IceBlockSwitch")
			continue
		for item_child in item.get_children():
			# sprite和tilemap都视为背景
			if item_child is Sprite:
				var info = ModParseUtil.parse_sprite(item_child)
				ModParseUtil.try_parse_resource(item_child,info,top_json)
				temp_json["bg"].append(info)
				pass
			if  item_child is TileMap:
				var info = ModParseUtil.parse_tilemap(item_child)
				ModParseUtil.try_parse_resource(item_child,info,top_json)
				temp_json["bg"].append(info)
				pass
			# 说明是传送带阻拦
			if item_child.name == "ConveyorBelt":
				var convery_belt = parse_conveyor_belt(item,top_json)
				temp_json["conveyor_belt"] = convery_belt
				pass
			# 说明是碰撞体阻拦
			if item_child.name == "Collision":
				var collision = parse_collision(item,top_json)
				temp_json["collision"] = collision
				pass
			pass
		node_item.append(temp_json)
		pass
	
	return node_item
	pass

# 玩家触碰这个区域后，会产生一次特效，
static func parse_deep_bubble_area_special_effect(scene,top_json)->Array:
	if !scene.has_node("DeepBubbleSpecialEfect"): return []
	var deep_bubble_area:Node2D = scene.get_node("DeepBubbleSpecialEfect")
	var node_item = []
	
	for area_item in deep_bubble_area.get_children():
		# 其子项必须是area2d+tilemap 
		if !(area_item is Area2D): 
			printerr("错误！，一次性检测特效必须是area2d+tilemap")
			continue
		if area_item.get_child_count() <= 0:
			printerr("错误！，area2d 必须包含子项tilemap")
			continue
		if !((area_item.collision_mask != 64 || area_item.collision_layer != 131072) and !(area_item.collision_layer == 64 && area_item.collision_layer == 131072)):
			printerr("错误，你必须设置DeepBubbleSpecialEfect 的area2d节点collision_mask 为 7或者 18，且不能同时设置")
			continue
		var area_json = ModParseUtil.parse_area2d(area_item)
		var script_json = {}
		var child = []
		area_json["script_json"] = script_json
		area_json["child"] = child
		
		for child_item in area_item.get_children():
			if child_item is TileMap:
				var tilemap_json = ModParseUtil.parse_tilemap(child_item)
				ModParseUtil.try_parse_resource(child_item,tilemap_json,top_json)
				child.append(tilemap_json)
			pass
		
		
		script_json["sence_parent"] = str(area_item.sence_parent)
		
		script_json["h_frame"] = area_item.h_frame
		script_json["fps"] = area_item.fps
		script_json["texutre"] = ModParseUtil.try_parse_resource2(area_item.texutre,top_json)
		
		node_item.append(area_json)
	
	return node_item


# 
static func parse_dust_grass_special_effect(scene,top_json)->Array:
	if !scene.has_node("DustGrassSpecialEffect"): return []
	var dust_grass_special_effect:Node2D = scene.get_node("DustGrassSpecialEffect")
	var node_item = []
	
	for area_item in dust_grass_special_effect.get_children():
		# 其子项必须是area2d+tilemap 
		if !(area_item is Area2D): 
			printerr("错误！，一次性检测特效必须是area2d+tilemap")
			continue
		if area_item.get_child_count() <= 0:
			printerr("错误！，area2d 必须包含子项tilemap")
			continue
		if !((area_item.collision_mask != 64 || area_item.collision_layer != 131072) and !(area_item.collision_layer == 64 && area_item.collision_layer == 131072)):
			printerr("错误，你必须设置DustGrassSpecialEffect 的area2d节点collision_mask 为 7或者 18，且不能同时设置")
			continue
		var area_json = ModParseUtil.parse_area2d(area_item)
		var script_json = {}
		var child = []
		area_json["script_json"] = script_json
		area_json["child"] = child
		
		for child_item in area_item.get_children():
			if child_item is TileMap:
				var tilemap_json = ModParseUtil.parse_tilemap(child_item)
				ModParseUtil.try_parse_resource(child_item,tilemap_json,top_json)
				child.append(tilemap_json)
			pass
		script_json["sence_parent"] = str(area_item.sence_parent)
		
		script_json["layer_level"] = area_item.layer_level
		script_json["effect_h_frame"] = area_item.effect_h_frame
		script_json["fps"] = area_item.fps
		if area_item.effect != null:
			script_json["effect"] = ModParseUtil.try_parse_resource2(area_item.effect,top_json)
		if area_item.normal != null:
			script_json["normal"] = ModParseUtil.try_parse_resource2(area_item.normal,top_json)
		
		if area_item.dust_clear_tilemap != null:
			script_json["dust_clear_tilemap"] = str(area_item.dust_clear_tilemap)
		
		if area_item.in_audio != null:
			script_json["in_audio"] = area_item.in_audio.resource_path
		
		node_item.append(area_json)
	
	return node_item
# 解析草丛区域，分三个，普通的grass,tallgrass,dustgrass

static func parse_tall_grass_special_effect(scene,top_json)->Array:
	if !scene.has_node("TallGrassSpecialEffect"): return []
	var dust_grass_special_effect:Node2D = scene.get_node("TallGrassSpecialEffect")
	var node_item = []
	
	for area_item in dust_grass_special_effect.get_children():
		# 其子项必须是area2d+tilemap 
		if !(area_item is Area2D): 
			printerr("错误！，一次性检测特效必须是area2d+tilemap")
			continue
		if area_item.get_child_count() <= 0:
			printerr("错误！，area2d 必须包含子项tilemap")
			continue
		if !((area_item.collision_mask != 64 || area_item.collision_layer != 131072) and !(area_item.collision_layer == 64 && area_item.collision_layer == 131072)):
			printerr("错误，你必须设置TallGrassSpecialEffect 的area2d节点collision_mask 为 7或者 18，且不能同时设置")
			continue
		var area_json = ModParseUtil.parse_area2d(area_item)
		var script_json = {}
		var child = []
		area_json["script_json"] = script_json
		area_json["child"] = child
		
		for child_item in area_item.get_children():
			if child_item is TileMap:
				var tilemap_json = ModParseUtil.parse_tilemap(child_item)
				ModParseUtil.try_parse_resource(child_item,tilemap_json,top_json)
				child.append(tilemap_json)
			pass
		script_json["sence_parent"] = str(area_item.sence_parent)
		
		script_json["layer_level"] = area_item.layer_level
		
		if area_item.normal != null:
			script_json["normal"] = ModParseUtil.try_parse_resource2(area_item.normal,top_json)
		
		if area_item.in_audio != null:
			script_json["in_audio"] = area_item.in_audio.resoutce_path
		
		node_item.append(area_json)
	
	return node_item
# 解析草丛区域，分三个，普通的grass,tallgrass,dustgrass

static func parse_light_grass_special_effect(scene,top_json)->Array:
	if !scene.has_node("LightGrassSpecialEffect"): return []
	var dust_grass_special_effect:Node2D = scene.get_node("LightGrassSpecialEffect")
	var node_item = []
	
	for area_item in dust_grass_special_effect.get_children():
		# 其子项必须是area2d+tilemap 
		if !(area_item is Area2D): 
			printerr("错误！，一次性检测特效必须是area2d+tilemap")
			continue
		if area_item.get_child_count() <= 0:
			printerr("错误！，area2d 必须包含子项tilemap")
			continue
		if !((area_item.collision_mask != 64 || area_item.collision_layer != 131072) and !(area_item.collision_layer == 64 && area_item.collision_layer == 131072)):
			printerr("错误，你必须设置LightGrassSpecialEffect 的area2d节点collision_mask 为 7或者 18，且不能同时设置")
			continue
		var area_json = ModParseUtil.parse_area2d(area_item)
		var script_json = {}
		var child = []
		area_json["script_json"] = script_json
		area_json["child"] = child
		
		for child_item in area_item.get_children():
			if child_item is TileMap:
				var tilemap_json = ModParseUtil.parse_tilemap(child_item)
				ModParseUtil.try_parse_resource(child_item,tilemap_json,top_json)
				child.append(tilemap_json)
			pass
		script_json["sence_parent"] = str(area_item.sence_parent)
		
		script_json["layer_level"] = area_item.layer_level
		script_json["map_id"] = area_item.map_id
		script_json["ignore_trigger_state"] = area_item.ignore_trigger_state
		script_json["transmit_position"] = [area_item.transmit_position.x,area_item.transmit_position.y]
		if area_item.normal_texture != null:
			script_json["normal_texture"] = ModParseUtil.try_parse_resource2(area_item.normal_texture,top_json)
		if area_item.active_texture != null:
			script_json["active_texture"] = ModParseUtil.try_parse_resource2(area_item.active_texture,top_json)
		if area_item.in_audio != null:
			script_json["in_audio"] = area_item.in_audio.resoutce_path
		node_item.append(area_json)
		
	return node_item
# 解析草丛区域，分三个，普通的grass,tallgrass,dustgrass


static func parse_all_grass(scene,top_json):
	if !scene.has_node("AllGrass"): return []
	var audio:Node2D = scene.get_node("AllGrass")
	
	var node_item = []
	for item_area in audio.get_children():
		if !item_area is Area2D: 
			printerr("错误，草丛特效区域必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为草丛的特效区域")
			continue
		if !((item_area.collision_mask != 64 || item_area.collision_layer != 131072) and !(item_area.collision_layer == 64 && item_area.collision_layer == 131072)):
			printerr("错误，你必须设置AllGrass 的area2d节点collision_mask 为 7或者 18，且不能同时设置")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		area_info["name"] = item_area.name
		var child = []
		var script_json = {}
		area_info["child"] = child
		area_info["script_json"] = script_json
		for item in item_area.get_children():
			if !(item is TileMap): 
				printerr("错误，草丛特效区域的 area2d子节点必须是 tilemap")
				continue
			var tilemap_info = ModParseUtil.parse_tilemap(item)
			ModParseUtil.try_parse_resource(item,tilemap_info,top_json)
			tilemap_info["name"] = item.name
			child.append(tilemap_info)
		# 读取脚本信息
		script_json["type"] = item_area.type
		script_json["only_operation_player"] = item_area.only_operation_player
		script_json["ice_block_submit_signal"] = item_area.ice_block_submit_signal
		script_json["dust_clear_tilemap"] = str(item_area.dust_clear_tilemap)
		script_json["map_id"] = item_area.map_id
		script_json["block_type"] = item_area.block_type
		script_json["sence_parent"] = str(item_area.sence_parent)
		if script_json["sence_parent"].empty():
			printerr("错误，sence_parent 不能为空")
			continue
		node_item.append(area_info)
	return node_item
	pass

# 解析遮罩修复区域问题
static func parse_mask_special_node(scene,top_json)->Array:
	if !scene.has_node("MaskSpecialNode"): return []
	var audio:Node2D = scene.get_node("MaskSpecialNode")
	var node_item = []
	for item_area in audio.get_children():
		if !item_area is Area2D: 
			printerr("错误，遮罩修复区域必须是一个area+collision_polygon2d组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个polygon2d来作为遮罩修复区域")
			continue
		if (item_area.collision_mask != 64 && item_area.collision_mask != 131072) and (item_area.collision_mask == 64 && item_area.collision_mask == 131072):
		# 这个判断有问题
#		if !((item_area.collision_mask != 64 || item_area.collision_mask != 131072) and !(item_area.collision_mask == 64 && item_area.collision_mask == 131072)):
			printerr("错误，你必须设置MaskSpecial 的area2d节点collision_mask 为 7或者 18，且不能同时设置")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		area_info["name"] = item_area.name
		var child = []
		area_info["child"] = child
		for item in item_area.get_children():
			if item is TileMap:
				var tilemap_json = ModParseUtil.parse_tilemap(item)
				ModParseUtil.try_parse_resource(item,tilemap_json,top_json)
				child.append(tilemap_json)
			if item is CollisionPolygon2D:
				var polygon2d_json = ModParseUtil.parse_collision_polygon2d(item)
				child.append(polygon2d_json)
			pass
		node_item.append(area_info)
	return node_item


# 解析通知行为的block_btn
# 这里只有一个node2d节点和 一个实体类，只需要解析实体类的脚本数据即可
static func parse_block_btn(scene,top_json)->Array:
	if !scene.has_node("BlockBtn"): return []
	var block_btn:Node2D = scene.get_node("BlockBtn")
	var node_item = []
	for item in block_btn.get_children():
		var item_json = {}
		var script_json = {}
		item_json["script_json"] = script_json
		item_json["name"] = item.name
		item_json["position"] = [item.position.x,item.position.y]
		script_json["notifaction_node_path"] = []
		for node_path in item.notifaction_node_path:
			script_json["notifaction_node_path"].append(str(node_path))
		script_json["collision_w"] = item.collision_w
		script_json["collision_h"] = item.collision_h
		script_json["active"] = item.active
		script_json["active_influence"] = item.active_influence
		script_json["collision_layer_level"] = item.collision_layer_level
		script_json["sprite_offset"] = [item.sprite_offset.x,item.sprite_offset.y]
		script_json["on_texture"] = ModParseUtil.try_parse_resource2(item.on_texture,top_json)
		script_json["off_texture"] = ModParseUtil.try_parse_resource2(item.off_texture,top_json)
		node_item.append(item_json)
		pass
	
	return node_item
	pass
# 解析被通知行为的block_collision
# 这里只有一个node2d节点和 一个实体类，只需要解析实体类的脚本数据即可
static func parse_block_collision(scene,top_json):
	if !scene.has_node("BlockCollision"): return []
	var block_collision:Node2D = scene.get_node("BlockCollision")
	var node_item = []

	for item in block_collision.get_children():
		var item_json = {}
		var script_json = {}
		item_json["script_json"] = script_json
		item_json["name"] = item.name
		item_json["position"] = [item.position.x,item.position.y]
		script_json["collision_w"] = item.collision_w
		script_json["collision_h"] = item.collision_h
		script_json["active"] = item.active
		script_json["collision_layer_level"] = item.collision_layer_level
		script_json["sprite_offset"] = [item.sprite_offset.x,item.sprite_offset.y]
		script_json["on_texture"] = ModParseUtil.try_parse_resource2(item.on_texture,top_json)
		script_json["off_texture"] = ModParseUtil.try_parse_resource2(item.off_texture,top_json)
		node_item.append(item_json)
		pass
	return node_item
	pass
# 解析对话框的组件
static func parse_active_block(scene,top_json):
	var node_item = []
	if !scene.has_node("Active"): return node_item
	var block_collision:Node2D = scene.get_node("Active")
	for item in block_collision.get_children():
		var script_json = {}
		var item_json = ModParseUtil.parse_area2d(item)
		item_json["script_json"] = script_json
		script_json["dialog_id"] = item.dialog_id
		script_json["collider"] = item.collider
		node_item.append(item_json)
		pass
	return node_item
# 解析地图上的灯光
static func parse_light(scene,top_json):
	var node_item = []
	if !scene.has_node("Light"): return node_item
	var light:Node2D = scene.get_node("Light")
	for item in light.get_children():
		var item_json = ModParseUtil.parse_light2d(item)
		item_json["texture"] = ModParseUtil.try_parse_resource2(item.texture,top_json)
		# 说明有脚本
		if item.get_script() != null:
			item_json["light_type"] = item.type
			var script_json = {}
			if item.type == "Flicker":
				script_json["min_value"] = item.min_value
				script_json["max_value"] = item.max_value
			item_json["script"] = script_json
			pass
		node_item.append(item_json)
		pass
	return node_item

# 这个解析的是场景和遮罩共用方法
static func deep_parse_no_script(item,top_json):
	# 没有子类了就返回一个空
	# 这里存储着这个节点的信息
	var node_item = {}
	if item == null: return {}
	# 如果有子类
	if item.get_child_count() > 0:
		if item is Node2D:
			node_item["z_index"] = item.z_index
		node_item["child"] = {}
		for child in item.get_children():
			var temp = deep_parse_no_script(child,top_json)
			if !temp.empty(): 
				temp["sort"] = child.get_index()
				node_item["child"][child.name] = temp
	if !(item is Sprite) and !(item is TextureRect) and !(item is TileMap) and !(item is Area2D) and !(item is CollisionPolygon2D) and !(item is ColorRect): return node_item
	if item is TileMap:
		node_item.merge(ModParseUtil.parse_tilemap(item),true)
	if item is Sprite:
		node_item.merge(ModParseUtil.parse_sprite(item),true)
	if item is TextureRect:
		node_item.merge(ModParseUtil.parse_texture_rect(item),true)
	if item is Area2D:
		node_item.merge(ModParseUtil.parse_area2d(item),true)
	if item is CollisionPolygon2D:
		node_item.merge(ModParseUtil.parse_collision_polygon2d(item),true)
	if item is ColorRect:
		node_item.merge(ModParseUtil.parse_color_rect(item),true)
	# 尝试解析资源
	ModParseUtil.try_parse_resource(item,node_item,top_json)
	return node_item

# 解析七岛石头机关打开山洞
static func parse_stone_switch(scene,top_json:Dictionary)->Array:
	if !scene.has_node("StoneSwitch"): return []
	var state_action_move:Node2D = scene.get_node("StoneSwitch")
	
	var node_item = []
	for item_area in state_action_move.get_children():
		if !item_area is Area2D: 
			printerr("错误，可行走层必须是一个area+tilemap组成，不能是其他节点类型")
			continue
		if item_area.get_children().size() <= 0:
			printerr("错误，你必须设置一个tilemap来作为行走的区域")
			continue
		if item_area.collision_layer != 2097152:
			printerr("错误，你必须设置StateActionMove 的area2d节点collision_layer 为 10")
			continue
		var area_info = ModParseUtil.parse_area2d(item_area)
		var child = []
		var script_json = {}
		area_info["child"] = child
		area_info["script_json"] = script_json
		for tilemap in item_area.get_children():
			if !(tilemap is TileMap): continue
			var tilemap_info = ModParseUtil.parse_tilemap(tilemap)
			ModParseUtil.try_parse_resource(tilemap,tilemap_info,top_json)
			child.append(tilemap_info)
		# 读取脚本信息
		script_json["pos_tilemap"] = item_area.pos_tilemap
		script_json["drama_id"] = item_area.drama_id
		
		node_item.append(area_info)
	return node_item
	pass
